package com.minivision.sdktestdemo.event;

import android.content.SharedPreferences;
import android.net.Uri;

import com.minivision.sdktestdemo.bean.MusicBean;

import java.io.Serializable;

/**
 * Created by devtang on 2016/4/15.
 */
public class PlayMusicEvent implements Serializable, Cloneable {
    //下面是几种常用状态
    public static final int EMPTY = 0;
    public static final int PLAY = 1;
    public static final int PAUSE = 2;
    public static final int NEXT = 3;
    public static final int PREV = 4;
    public static final int STOP = 5;
    public static final int SEEK = 6;

    //唯一标识(地址)
    public String mUri;
    public String mName;
    public String mSubName;
    public Uri mAlbumUri;
    public int mStatus;
    public int mDuration;
    public int mCurPos;
    //播放顺序
    public int mOrder;

    public static final String MUSIC_URI = "music_loading";
    public static final String NAME = "name";
    public static final String SUB_NAME = "subname";
    public static final String DURATION = "duration";
    private static final String ORDER = "order";

    public PlayMusicEvent(MusicBean bean, int status) {
        this.mUri = bean.getmPath();
        this.mName = bean.getmMusicName();
        this.mSubName = bean.getmSingerName();
        this.mStatus = status;
        this.mAlbumUri = bean.getmPicUri();
        this.mDuration = bean.getmLen();
        mCurPos = 0;
    }

    public PlayMusicEvent(String mUri, String mName, String mSubName, int status, Uri albumUri, int duration) {
        this.mUri = mUri;
        this.mName = mName;
        this.mSubName = mSubName;
        this.mStatus = status;
        this.mAlbumUri = albumUri;
        this.mDuration = duration;
        mCurPos = 0;
    }

    public PlayMusicEvent(SharedPreferences preferences) {
        load(preferences);
    }

    public PlayMusicEvent() {
    }

    public void setPlayEvent(PlayMusicEvent event) {
        this.mName = event.mName;
        this.mSubName = event.mSubName;
        this.mUri = event.mUri;
        this.mAlbumUri = event.mAlbumUri;
        this.mStatus = event.mStatus;
        this.mCurPos = event.mCurPos;
        this.mDuration = event.mDuration;
    }

    public void load(SharedPreferences preferences) {
        mName = preferences.getString(NAME, "");
        mSubName = preferences.getString(SUB_NAME, "");
        mUri = preferences.getString(MUSIC_URI, "");
        mDuration = preferences.getInt(DURATION, 0);
        mOrder = preferences.getInt(ORDER, 0);
        mStatus = STOP;
        mCurPos = 0;
        mAlbumUri = null;
    }

    public void save(SharedPreferences preferences) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(ORDER, mOrder);
        editor.putString(MUSIC_URI, mUri);
        editor.putString(NAME, mName);
        editor.putString(SUB_NAME, mSubName);
        editor.putInt(DURATION, mDuration);
        editor.commit();
    }
}
